package com.ikingtech.platform.service.plugin.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.plugin.entity.PluginDO;
import com.ikingtech.platform.service.plugin.mapper.PluginMapper;
import com.ikingtech.platform.service.plugin.model.PluginQueryParamDTO;
import org.springframework.stereotype.Service;

@Service
public class PluginService extends ServiceImpl<PluginMapper, PluginDO> {


    public PageResult<PluginDO> listPage(PluginQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<PluginDO>query().lambda()
                .like(Tools.Str.isNotBlank(queryParam.getName()), PluginDO::getName, queryParam.getName())
                .like(Tools.Str.isNotBlank(queryParam.getRemark()), PluginDO::getRemark, queryParam.getRemark())
                .orderByDesc(PluginDO::getCreateTime)));
    }

    public boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<PluginDO>query().lambda().eq(PluginDO::getId, id));
    }

    public boolean nameExist(String name) {
        return this.baseMapper.exists(Wrappers.<PluginDO>query().lambda()
                .isNotNull(PluginDO::getId)
                .eq(PluginDO::getName, name));
    }

    public boolean nameExist(String id, String name) {
        return this.baseMapper.exists(Wrappers.<PluginDO>query().lambda()
                .ne(PluginDO::getId, id)
                .eq(PluginDO::getName, name));
    }
}
